package net.hxz.modules.controller.backEnd;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import net.hxz.commons.global.Global;
import net.hxz.modules.controller.BaseController;
import net.hxz.modules.pojo.BackEndUser;
import net.hxz.modules.pojo.Column;
import net.hxz.modules.services.IColumnService;
import net.hxz.modules.services.IUserService;
import net.wy.commons.util.TypeConversionUtil;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("backEnd")
public class BackEndCoreController extends BaseController{
	private final String PATH = "backEnd/modules/core/";
	
	@Autowired
	private IColumnService columnService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="login", method=RequestMethod.GET)
	public String toLogin() {
		if (session.getAttribute(Global.SESSION_ROOT) != null) {
			return "redirect:/backEnd/main";
		}
		return PATH + "login";
	}
	
	@RequestMapping(value="login", method=RequestMethod.POST)
	public String loginOK(String name, String pwd, ModelMap map){
		if (StringUtils.isBlank(name) || StringUtils.isBlank(pwd)) {
			return PATH + "loginFail";
		}
		BackEndUser root = this.userService.getBackEndUser(new BackEndUser(name, pwd));
		if (root == null) {
			return PATH + "loginFail";
		}
		session.setAttribute(Global.SESSION_ROOT, root);
		return "redirect:/backEnd/main";
	}
	
	@RequestMapping("logOut")
	public String logOut(){
		session.removeAttribute(Global.SESSION_ROOT);
		return "redirect:/backEnd/login";
	}
	
	@RequestMapping(value = "main")
    public String main() {
        return PATH + "main";
    }
	
    @RequestMapping("loadTree")
    public String loadTree(String id, ModelMap map) {
        try {
            int columnId = 1;
            if (StringUtils.isNotBlank(id)) {
                JSONArray arrays = new JSONArray();
                columnId = TypeConversionUtil.parseInt(id);
                List<Column> columns = columnService.getByParentId(columnId);
                for (Column column : columns) {
                    JSONObject tempObject = new JSONObject();
                    tempObject.put("name", column.getName());
                    tempObject.put("id", column.getId());
                    tempObject.put("link", column.getUrl());
                    int size = columnService.getColumnCountByParentId(column.getId());
                    if (size > 0) {
                        tempObject.put("isParent", true);
                    }
                    arrays.put(tempObject);
                }
                map.put("result", arrays.toString());
            } else {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", 1);
                jsonObject.put("name", "后台管理");
                jsonObject.put("open", true);
                jsonObject.put("isParent", true);
                map.put("result", jsonObject.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return PATH + "result";
    }
	
	/**
	 * 后台ck上传图片
	 */
	@RequestMapping(value = "/customUpload")
	public void customAnswer(@RequestParam("upload") MultipartFile file, ModelMap map) {
		if(!file.getContentType().matches("^image/.*")){
			map.put("message", "上传失败");
		}
		String path = request.getSession().getServletContext().getRealPath("/");
		File toFile =new File(path + "upload/" + new Date().getTime());
		try {
			file.transferTo(toFile);
			map.put("hasError", false);
			map.put("url", toFile.getName());
		} catch (IOException e) {
			map.put("hasError", true);
			map.put("message", "上传失败");
			e.printStackTrace();
		}
	}
}

